package com.goods.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.mapper.ConsumerMapper;
import com.goods.business.service.ConsumerService;
import com.goods.common.model.business.Consumer;
import com.goods.common.vo.business.ConsumerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @description：
 * @version:
 * @author：ruixinag
 * @create：Created in 2021/11/9 16:06
 */
@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    private ConsumerMapper consumerMapper;

    //物资去处分页列表展示
    @Override
    public Map findConsumerList(Map paramMap) {
        String pageNum = (String) paramMap.get("pageNum");
        String pageSize = (String) paramMap.get("pageSize");
        String name = (String) paramMap.get("name");
        String address = (String) paramMap.get("address");
        String contact = (String) paramMap.get("contact");

        Example example = new Example(Consumer.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(name)) {
            criteria.andLike("name", "%" + name + "%");
        }
        if (!StringUtils.isEmpty(address)) {
            criteria.andLike("address", "%" + address + "%");
        }
        if (!StringUtils.isEmpty(contact)) {
            criteria.andLike("contact", "%" + contact + "%");
        }


        //分页插件
        PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        List<Consumer> consumers = consumerMapper.selectByExample(example);
        PageInfo<Consumer> pageInfo = new PageInfo<>(consumers);

        List<ConsumerVO> list = pageInfo.getList().stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());
        //对数据进行排序
        Collections.sort(list, (o1, o2) -> {
            return o1.getSort() - o2.getSort();
        });

        HashMap<String, Object> result = new HashMap<>();
        result.put("rows", list);
        result.put("total",pageInfo.getTotal());
        return result;
    }

    //添加物资去处
    @Override
    public void add(ConsumerVO consumerVO) {
        consumerVO.setCreateTime(new Date());
        consumerVO.setModifiedTime(new Date());
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO, consumer);
        consumerMapper.insertSelective(consumer);
    }

    //数据回显
    @Override
    public ConsumerVO edit(Long consumerId) {
        Consumer consumer = consumerMapper.selectByPrimaryKey(consumerId);
        ConsumerVO consumerVO = new ConsumerVO();
        BeanUtils.copyProperties(consumer, consumerVO);
        return consumerVO;
    }

    //更新数据
    @Override
    public void update(ConsumerVO consumerVO) {
        Consumer consumer = new Consumer();
        BeanUtils.copyProperties(consumerVO, consumer);
        consumer.setModifiedTime(new Date());
        consumerMapper.updateByPrimaryKeySelective(consumer);
    }

    //删除
    @Override
    public void delete(Long consumerId) {
        consumerMapper.deleteByPrimaryKey(consumerId);
    }

    //返回全部去向
    @Override
    public List<ConsumerVO> findAll() {
        List<Consumer> consumers = consumerMapper.selectAll();
        return consumers.stream().map(consumer -> {
            ConsumerVO consumerVO = new ConsumerVO();
            BeanUtils.copyProperties(consumer, consumerVO);
            return consumerVO;
        }).collect(Collectors.toList());

    }
}
